<?php
/**
 * initialise block
 *
 * @author       The PostNuke Development Team
 */
function InnoForum_CategorySubcategoryListblock_init()
{

}

/**
 * get information on block
 *
 * @author       The PostNuke Development Team
 * @return       array       The block information
 */
function InnoForum_CategorySubcategoryListblock_info()
{
    return array('text_type'      => 'Category & Subcategory list',
                 'module'         => 'InnoForum',
                 'text_type_long' => 'list_category_subcategory',
                 'allow_multiple' => true,
                 'form_content'   => false,
                 'form_refresh'   => false,
                 'show_preview'   => true);
}

/**
 * display block
 *
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function InnoForum_CategorySubcategoryListblock_display($blockinfo)
{

    $modname  = 'InnoForum';
    $class    = 'Subcategory';

    $vars = pnBlockVarsFromContent($blockinfo['content']);
    $modinfo = pnModGetInfo(pnModGetIDFromName($modname));
    $directory = $modinfo['directory'];

    pnModDBInfoLoad($modname, $directory);

    //load render
    $render = pnRender::getInstance($modname);


    ////////////////////////////////////
    //Query Category
    ////////////////////////////////////
    //Get table information
    $pntables = pnDBGetTables();
    
    //Get information category table    
    $tableCategory  = $pntables['innoforum_category'];
    $columncategory = $pntables['innoforum_category_column'];
    
    //Get information subcatogory table
    $tableSubCategory  = $pntables['innoforum_subcategory'];
    $columnSubCategory = $pntables['innoforum_subcategory_column'];     


    //Query all category
    $sql = "SELECT
              $tableCategory.$columncategory[id],  
              $tableCategory.$columncategory[name]
            FROM
              $tableCategory
            ORDER BY 
              $tableCategory.$columncategory[id] ASC ";
    
    $column = array("category_id", "category_name");
    $result = DBUtil::executeSQL($sql);
    $categoryArray = DBUtil::marshallObjects ($result, $column);
 
 
 
    ////////////////////////////////////
    //Query SubCategory
    ////////////////////////////////////
    $objectArray = array();
    if ($categoryArray) {
      $count = 0;
      foreach($categoryArray as $item){   
      
        $category_id = $item['category_id'];
        //Query all subcategory in category
        $sql = "SELECT
                  $tableSubCategory.$columnSubCategory[id],  
                  $tableSubCategory.$columnSubCategory[name]  
                FROM
                  $tableSubCategory 
                WHERE
                  $tableSubCategory.$columnSubCategory[category_id] = ".$category_id ."
                ORDER BY
                  $tableSubCategory.$columnSubCategory[name] ASC ";
    
        $column = array("subcategory_id", "subcategory_name");
        $result = DBUtil::executeSQL($sql);  
          
        $objectArray[$count]["category_id"] = $item['category_id'];         
        $objectArray[$count]["category_name"] = $item['category_name'];        
        $objectArray[$count]["subcategory"] = DBUtil::marshallObjects ($result, $column);
        $count++;
      }
    }
     
      
    //print_r($objectArray); exit;
      
    if ($objectArray) {
        $render->assign('categorySubcategoryArray', $objectArray); 
    } else {
        $render->assign('categorySubcategoryArray', false); 
    }


    // Populate block info and pass to theme
    $blockinfo['content'] = $render->fetch('block_user_categorysubcategorylist.htm');
    return themesideblock($blockinfo);

}


/**
 * modify block settings
 *
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function InnoForum_CategorySubcategoryListblock_modify($blockinfo)
{
  $vars = pnBlockVarsFromContent($blockinfo['content']);
  //load render
	return '';
}


/**
 * update block settings
 *
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function InnoForum_CategorySubcategoryListblock_update($blockinfo)
{

    // Get current content
    $vars = pnBlockVarsFromContent($blockinfo['content']);

    // write back the new contents
    $blockinfo['content'] = pnBlockVarsToContent($vars);

    // clear the block cache
    $pnRender = pnRender::getInstance('InnoForum');
    $pnRender->clear_cache('block_user_categorysubcategorylist.htm');

    return $blockinfo;
}
